<?php
declare (strict_types = 1);

namespace app\controller;

use app\model\Test87s;
use think\Request;
use app\validate\User;
use think\exception\ValidateException;
class Test87
{
    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
     * 返回渲染视图层，添加页面
     */
    public function add(){
        return view('./test87add');
    }

    /**
     * @param Request $request
     * 添加功能
     * validate验证
     * 接收数据，调用模型层添加方法，提示处理结果,跳转页面
     */
    public function adddo(Request $request){
        try {
            validate(User::class)->check([
                'user_name'  => "$request[user_name]",
                'user_sex'  => "$request[user_sex]",
                'email' => "$request[email]",
            ]);
        } catch (ValidateException $e) {
            dump($e->getError());
            header("refresh:2;url=add");
            die();
        }
        $data['user_name']  =  $request['user_name'];
        $data['user_sex']   =  $request['user_sex'];
        $data['email']      =  $request['email'];
        $res = Test87s::addOne($data);
        if($res){
            echo "<script>alert('添加成功')</script>";
            header("refresh:0;url=add");
        }else{
            echo "<script>alert('添加失败')</script>";
            header("refresh:0;url=add");
        }
    }
    public function show(Request $request){
        $where = [];
        if($request['user_name']){
            $where[] = ['user_name','like',"%$request[user_name]%"];
        }
        $data = Test87s::getAll($where);
        return view('./test87show',['data'=>$data]);
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
